<template>
  <div>
    <el-dialog
      title="新增学科"
      :visible="dialogVisible"
      width="30%"
      @close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="ruleForm" label-width="80px">
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model="ruleForm.subjectName"></el-input>
        </el-form-item>
        <el-form-item label="即时配送" prop="delivery">
          <el-switch
            v-model="ruleForm.isFrontDisplay"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="addPost">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/subjects'
export default {
  data () {
    return {
      ruleForm: {
        isFrontDisplay: 1,
        subjectName: ''
      },
      rules: {
        subjectName: [
          { required: true, message: '请输入学科', trigger: 'blur' }
        ]
      }
    }
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handleClose () {
      this.$emit('handle-close')
      this.ruleForm = {
        isFrontDisplay: 1,
        subjectName: ''
      }
      this.$refs.form.resetFields()
    },
    addPost () {
      this.$refs.form.validate(async (vali) => {
        if (vali) {
          if (this.ruleForm.id) {
            await update(this.ruleForm)
            this.$message.success('修改成功!')
          } else {
            await add(this.ruleForm)
            this.$message.success('添加成功!')
          }
          this.handleClose()
        }
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
